package com.example.demo.Service;

import com.example.demo.dao.Game_dao;
import com.example.demo.module.User;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.awt.*;
import java.util.HashMap;

@Service
public class Userservicelml implements Userservice {
    @Resource
    Game_dao game_dao;

    @Override
    public String form_login(User user){
        String resstr="";
        //User u= Wc_lhtDao.findRjuserbystunum(user.getStunum());
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u==null){
            resstr = "用户不存在";
        }
        if(u!=null) {
            if (u.getStupwd().equals(DigestUtils.md5DigestAsHex(user.getStupwd().getBytes()))) {
                resstr = "成功";
            } else {
                resstr = "密码错误";
            }
        }
        return resstr;
    }

    @Override
    public String add_user(User user){
        if(game_dao.findwc_lhtbystunum(user.getStunum())!=null)return "用户已存在";
        try {
            game_dao.addwc_lhtform(user.getStunum(), DigestUtils.md5DigestAsHex(user.getStupwd().getBytes()), 0, 0, 0);
            return "成功";
        }catch (Exception e){
            return "失败";
        }

    }


     public boolean updatestupwd(User user,String password){
        try{
//            game_dao.update_lhtform(user.getEmail(),password);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public HashMap<Integer, Integer> getscore(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        HashMap<Integer,Integer> hashMap=new HashMap<Integer,Integer>();
        hashMap.put(0,u.getSnake());
        hashMap.put(1,u.getTantanqiu());
        hashMap.put(2,u.getFlybird());
        hashMap.put(3,u.getPlanewar());
        hashMap.put(4,u.getParkour());
        return hashMap;
    }

    @Override
    public void updatesnake(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u.getSnake()<user.getSnake()) {
            game_dao.updatesnake(user.getStunum(), user.getSnake());
        }
    }

    @Override
    public void updateball(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u.getTantanqiu()<user.getTantanqiu()) {
            game_dao.updateball(user.getStunum(), user.getTantanqiu());
        }
    }

    @Override
    public void updateflybird(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u.getFlybird()<user.getFlybird()) {
            game_dao.updateflybird(user.getStunum(), user.getFlybird());
        }
    }

    @Override
    public void updateplanewar(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u.getPlanewar()<user.getPlanewar()) {
            game_dao.updateplanewar(user.getStunum(), user.getPlanewar());
        }
    }

    @Override
    public void updataparkour(User user) {
        User u=game_dao.findwc_lhtbystunum(user.getStunum());
        if(u.getParkour()<user.getParkour()){
            game_dao.updataparkour(user.getStunum(),user.getParkour());
        }
    }

}